﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gallery.MVC.Data;
using Gallery.MVC.Data.DataAccess;

namespace Gallery.MVC.Services
{
    public class CatalogService
    {
        ICatalogRepository _repository = null;

        public CatalogService(ICatalogRepository repository)
        {
            _repository = repository;
            if (_repository == null)
                throw new System.InvalidOperationException("CategoryService repository == null");
        }


        public IList<Category> GetCategories()
        {
            IList<Category> rawCategory = _repository.GetCategories().ToList();

            var parents = (from c in rawCategory
                            where c.ParentID == 0
                            select c).ToList();

            parents.ForEach(p =>
            {
                p.SubCategories = (from subs in rawCategory
                                   where subs.ParentID == p.ID
                                   select subs).ToList();
            });

            return parents;
        }
    }
}
